<?php

namespace App\Models\Chanjet;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class TplusOtherOutboundOrder extends Model
{
    protected $table = 'tplus_other_outbound_order';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    public static function getList(Request $request, $memberinfo, $isgetall)
    {
        $cpid = $memberinfo['cpid'] ?? 0;
        $where[] = ['cpid',$cpid];
        $query = self::with("info")->where($where)->orderBy("voucher_time","desc");

        $start_time = isTimestamp($request->start_time)?$request->start_time:strtotime($request->start_time);
        $end_time = isTimestamp($request->end_time)?$request->end_time:strtotime($request->end_time);
        if($start_time && $end_time){
            $query->whereBetween("voucher_time",[$start_time,$end_time]);
        }
        if($request->voucher_code != ''){
            $query->where('voucher_code', 'like', '%' . $request->voucher_code . '%');
        }

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }

    }
    public function info()
    {
        return $this->hasMany(TplusOtherOutboundOrderInfo::class,'main_id','id');
    }
}
